<template>
  <div>
    <van-nav-bar fixed placeholder safe-area-inset-top>
      <template #left>
        <img src="../../assets/toutiao_logo.png" alt="" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" @click="$router.push('/search')"/>
      </template>
    </van-nav-bar>
    <!-- 编辑频道图标 -->
    <van-icon
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
      @click="showPopup"
    />
    <van-popup v-model="show">
      <ChannelEdit
        @closeEvent="closeFn"
        :userChannelList="userChannel"
        @addUserEvent="addUserFn"
        @delChannelEvent="delChannelFn"
        @showEvent="showFn"
        v-model="active"
      ></ChannelEdit>
    </van-popup>
    <van-tabs
    animated
    v-model="active"
    sticky
    offset-top="1.226667rem"
    @change="changeTabsFn"
     :before-change="toggleTabsFn"
   >
      <van-tab
        :title="obj.name"
        v-for="obj in userChannel"
        :key="obj.id"
        :name="obj.id"
      >
        <ArticleList :channelId="active"></ArticleList>
      </van-tab>
    </van-tabs>
    <van-action-sheet
      v-model="$store.state.isShow"
      :actions="actions"
      @select="onSelect"
      @close="closeSheetFn"
      get-container="body"
      :cancel-text="text"
      @cancel="onCancel"
    />
  </div>
</template>

<script>

import ChannelEdit from './components/ChannelEdit.vue'
import {
  getUserChannelAPI,
  dislike,
  reportsAPI,
  setChannelAPI,
  delChannelAPI
} from '@/api'
import ArticleList from './ArticleList.vue'
import { mapState, mapMutations } from 'vuex'
import { Toast } from 'vant'
const newObj = {}
export default {
  data () {
    return {
      userChannel: [],
      active: 0,
      show: false,
      actions: [],
      text: '取消'
    }
  },
  computed: {
    ...mapState(['firstActions', 'secondsActions'])
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  async created () {
    const res = await getUserChannelAPI()
    this.userChannel = res.data.data.channels
    this.actions = this.firstActions
    window.addEventListener('scroll', () => {
      this.$route.meta.scrollT = document.documentElement.scrollTop
      // newObj[this.active] = document.documentElement.scrollTop
      // console.log(newObj)
    })
    // console.log(res)
  },
  activated () {
    console.log(this.$route.meta.scrollT)
    document.documentElement.scrollTop = this.$route.meta.scrollT
    this.isShowMutations(false)
  },
  methods: {
    // 切换标签前的回调函数
    toggleTabsFn () {
      newObj[this.active] = document.documentElement.scrollTop
      return true
    },
    changeTabsFn () {
      // console.log(newObj[this.active])
      setTimeout(() => {
        document.documentElement.scrollTop = newObj[this.active]
      }, 0)
    },
    showFn (bool) {
      this.show = bool
    },
    async delChannelFn (id) {
      if (id === 0) return Toast('推荐不能删')
      const res = await delChannelAPI(id)
      console.log(res)
      const index = this.userChannel.findIndex((obj) => obj.id === id)
      this.userChannel.splice(index, 1)
    },
    async addUserFn (obj) {
      this.userChannel.push(obj)
      const newArr = []
      this.userChannel.forEach((obj, index) => {
        const newObj = { ...obj }
        newObj.seq = index + ''
        delete newObj.name
        // console.log(newObj)
        newArr.push(newObj)
      })
      const res = await setChannelAPI(newArr)
      console.log(res)
    },

    showPopup () {
      this.show = true
    },
    closeFn (bool) {
      this.show = bool
    },
    closeSheetFn () {
      this.actions = this.firstActions
      this.text = '取消'
    },
    ...mapMutations(['isShowMutations']),
    async onSelect (item) {
      // console.log(item)
      if (item.name === '反馈垃圾内容') {
        this.actions = this.secondsActions
        this.text = '返回'
      } else if (item.name === '不感兴趣') {
        // console.log()
        const res = await dislike(this.$store.state.articleID)
        console.log(res)
        Toast.success('好的吧')
        this.isShowMutations(false)
      } else {
        await reportsAPI({
          target: this.$store.state.articleID,
          type: item.value
        })
        Toast.success('举报成功')
        setTimeout(() => {
          this.isShowMutations(false)
          this.actions = this.firstActions
        }, 2000)
      }
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // this.show = false
    },
    onCancel () {
      if (this.text === '返回') {
        this.text = '取消'
        this.actions = this.firstActions
        this.isShowMutations(true)
      }
    }
  }

}
</script>

<style scoped lang="less">
/deep/ .van-nav-bar__left img {
  height: 30px;
  width: 100px;
}
/deep/ .van-tab__pane {
  padding-bottom: 50px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}
/deep/ .van-popup {
  width: 100%;
  height: 100vh;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
</style>
